nanopyx.core.utils.easy_gui
A module to help simplify the create of GUIs in Jupyter notebooks using ipywidgets.
1""" 2A module to help simplify the create of GUIs in Jupyter notebooks using ipywidgets. 3""" 4 5import os 6import yaml 7import platform 8import numpy as np 9from ipyfilechooser import FileChooser 10from skimage.exposure import rescale_intensity 11 12# import cache if python >= 3.9, otherwise import lru_cache 13if platform.python_version_tuple() >= ("3", "9"): 14 from functools import cache 15else: 16 from functools import lru_cache as cache 17 18try: 19 import ipywidgets as widgets 20 from IPython import display as dp 21 from IPython.display import display 22 from matplotlib import pyplot as plt 23except ImportError: 24 print("jupyter optional-dependencies not installed, conside installing with 'pip install nanopyx[jupyter]'") 25 raise ImportError 26 27 28class EasyGui: 29 def __init__(self, title="basic_gui", width="50%"): 30 """ 31 Container for widgets. 32 :param width: width of the widget container 33 """ 34 self._layout = widgets.Layout(width=width) 35 self._style = {"description_width": "initial"} 36 self._widgets = {} 37 self._nLabels = 0 38 self._main_display = widgets.Output() 39 self._title = title 40 self._cfg = {title: {}} 41 self.cfg = self._cfg[title] 42 43 # Get the user's home folder 44 self._home_folder = os.path.expanduser("~") 45 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 46 if not os.path.exists(self._config_folder): 47 os.makedirs(self._config_folder) 48 49 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 50 if os.path.exists(self._config_file): 51 with open(self._config_file, "r") as f: 52 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 53 if title in self._cfg: 54 self.cfg = self._cfg[title] 55 56 def __getitem__(self, tag: str) -> widgets.Widget: 57 return self._widgets[tag] 58 59 def __len__(self) -> int: 60 return len(self._widgets) 61 62 def add_label(self, *args, **kwargs): 63 """ 64 Add a label widget to the container. 65 :param args: args for the widget 66 :param kwargs: kwargs for the widget 67 """ 68 self._nLabels += 1 69 self._widgets[f"label_{self._nLabels}"] = widgets.Label(*args, **kwargs, layout=self._layout, style=self._style) 70 71 def add_button(self, tag, *args, **kwargs): 72 """ 73 Add a button widget to the container. 74 :param tag: tag to identify the widget 75 :param args: args for the widget 76 :param kwargs: kwargs for the widget 77 """ 78 self._widgets[tag] = widgets.Button(*args, **kwargs, layout=self._layout, style=self._style) 79 80 def add_text(self, tag, *args, **kwargs): 81 """ 82 Add a text widget to the container. 83 :param tag: tag to identify the widget 84 :param args: args for the widget 85 :param kwargs: kwargs for the widget 86 """ 87 self._widgets[tag] = widgets.Text(*args, **kwargs, layout=self._layout, style=self._style) 88 89 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 90 """ 91 Add a integer slider widget to the container. 92 :param tag: tag to identify the widget 93 :param args: args for the widget 94 :param remember_value: remember the last value 95 :param kwargs: kwargs for the widget 96 """ 97 if remember_value and tag in self.cfg and kwargs["min"] <= self.cfg[tag] <= kwargs["max"]: 98 kwargs["value"] = self.cfg[tag] 99 self._widgets[tag] = widgets.IntSlider(*args, **kwargs, layout=self._layout, style=self._style) 100 101 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 102 """ 103 Add a float slider widget to the container. 104 :param tag: tag to identify the widget 105 :param args: args for the widget 106 :param remember_value: remember the last value 107 :param kwargs: kwargs for the widget 108 """ 109 if remember_value and tag in self.cfg: 110 kwargs["value"] = self.cfg[tag] 111 self._widgets[tag] = widgets.FloatSlider(*args, **kwargs, layout=self._layout, style=self._style) 112 113 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 114 """ 115 Add a checkbox widget to the container. 116 :param tag: tag to identify the widget 117 :param args: args for the widget 118 :param remember_value: remember the last value 119 :param kwargs: kwargs for the widget 120 """ 121 if remember_value and tag in self.cfg: 122 kwargs["value"] = self.cfg[tag] 123 self._widgets[tag] = widgets.Checkbox(*args, **kwargs, layout=self._layout, style=self._style) 124 125 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 126 """ 127 Add a integer text widget to the container. 128 :param tag: tag to identify the widget 129 :param args: args for the widget 130 :param remember_value: remember the last value 131 :param kwargs: kwargs for the widget 132 """ 133 if remember_value and tag in self.cfg: 134 kwargs["value"] = self.cfg[tag] 135 136 self._widgets[tag] = widgets.IntText( 137 *args, **kwargs, layout=self._layout, style=self._style) 138 139 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 140 """ 141 Add a float text widget to the container. 142 :param tag: tag to identify the widget 143 :param args: args for the widget 144 :param remember_value: remember the last value 145 :param kwargs: kwargs for the widget 146 """ 147 if remember_value and tag in self.cfg: 148 kwargs["value"] = self.cfg[tag] 149 self._widgets[tag] = widgets.FloatText( 150 *args, **kwargs, layout=self._layout, style=self._style) 151 152 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 153 """ 154 Add a dropdown widget to the container. 155 :param tag: tag to identify the widget 156 :param args: args for the widget 157 :param remember_value: remember the last value 158 :param kwargs: kwargs for the widget 159 """ 160 if remember_value and tag in self.cfg and self.cfg[tag] in kwargs["options"]: 161 kwargs["value"] = self.cfg[tag] 162 self._widgets[tag] = widgets.Dropdown(*args, **kwargs, layout=self._layout, style=self._style) 163 164 def add_file_upload(self, tag, *args, accept="*", multiple=False, **kwargs): 165 """ 166 Add a file upload widget to the container. 167 :param tag: tag to identify the widget 168 :param args: args for the widget 169 :param accept: file types to accept 170 :param multiple: allow multiple files to be uploaded 171 :param kwargs: kwargs for the widget 172 """ 173 self._widgets[tag] = FileChooser() 174 175 def save_settings(self): 176 # remember widget values for next time and store them in a config file 177 for tag in self._widgets: 178 if tag.startswith("label_"): 179 pass 180 elif hasattr(self._widgets[tag], "value"): 181 self.cfg[tag] = self._widgets[tag].value 182 self._cfg[self._title] = self.cfg 183 with open(self._config_file, "w") as f: 184 yaml.dump(self._cfg, f) 185 186 def show(self): 187 """ 188 Show the widgets in the container. 189 """ 190 # display the widgets 191 display(*self._widgets.values()) 192 193 def clear(self): 194 """ 195 Clear the widgets in the container. 196 """ 197 self._widgets = {} 198 self._nLabels = 0 199 self._main_display.clear_output() 200 201 202def view_image(image): 203 """ 204 Plot an image. 205 :param image: image to be plotted 206 """ 207 fig, ax = plt.subplots() 208 fig.canvas.header_visible = False 209 fig.canvas.footer_visible = False 210 211 ax.imshow(image) 212 plt.axis("off") 213 plt.draw() 214 215 216def view_image_stack(image, cmap="viridis"): 217 """ 218 Plot an image stack with dimensions >= 2. 219 Sliders to move across the dimensions are added. 220 :param cmap: colormap to be use to plot the image 221 """ 222 cm = plt.get_cmap(cmap) 223 dims = image.shape 224 params = {} 225 if len(dims) > 2: 226 for i in range(len(dims) - 2): 227 params["dim" + str(i)] = widgets.IntSlider( 228 min=0, max=dims[i] - 1, step=1, value=0, description="dim" + str(i) 229 ) 230 fig, ax = plt.subplots() 231 fig.canvas.header_visible = False 232 fig.canvas.footer_visible = False 233 234 def show_slice(**kwargs): 235 tmp_1 = rescale_intensity(image) 236 for k, value in kwargs.items(): 237 if k != "curtain": 238 tmp_1 = tmp_1[value] 239 ax.imshow(tmp_1, cmap=cmap) 240 plt.axis("off") 241 plt.draw() 242 243 widgets.interact(show_slice, **params) 244 245 246def view_curtain_stack(image_1: np.ndarray, image_2: np.ndarray, cmap: str = "viridis"): 247 """ 248 Plot two image stacks with dimensions >= 2. 249 Sliders to move across the dimensions are added. 250 251 :param image_1: Left image to be plotted on the curtain 252 :param image_2: Right image to be plotted on the curtain 253 :param cmap: Matplotlib colormap to be used to plot the images. Defaults to "viridis". 254 """ 255 assert image_1.shape == image_2.shape 256 dims = image_1.shape 257 params = {} 258 params["curtain"] = widgets.IntSlider( 259 value=image_1.shape[-1] / 2, min=0, max=image_1.shape[-1], description="Curtain" 260 ) 261 if len(dims) > 2: 262 for i in range(len(dims) - 2): 263 params["dim" + str(i)] = widgets.IntSlider( 264 min=0, max=dims[i] - 1, step=1, value=0, description="dim" + str(i) 265 ) 266 267 fig, ax = plt.subplots() 268 fig.canvas.header_visible = False 269 fig.canvas.footer_visible = False 270 271 def show_slice(**kwargs): 272 tmp_1 = rescale_intensity(image_1) 273 tmp_2 = rescale_intensity(image_2) 274 for k, value in kwargs.items(): 275 if k != "curtain": 276 tmp_1 = tmp_1[value] 277 tmp_2 = tmp_2[value] 278 279 for k, value in kwargs.items(): 280 if k == "curtain": 281 combined = np.zeros((image_1.shape[-2], image_1.shape[-1])) 282 combined[:, : int(value)] += tmp_1[:, : int(value)] 283 combined[:, int(value) :] += tmp_2[:, int(value) :] 284 ax.imshow(combined, cmap=cmap) 285 plt.axis("off") 286 plt.draw() 287 288 widgets.interact(show_slice, **params)
class
EasyGui:
29class EasyGui: 30 def __init__(self, title="basic_gui", width="50%"): 31 """ 32 Container for widgets. 33 :param width: width of the widget container 34 """ 35 self._layout = widgets.Layout(width=width) 36 self._style = {"description_width": "initial"} 37 self._widgets = {} 38 self._nLabels = 0 39 self._main_display = widgets.Output() 40 self._title = title 41 self._cfg = {title: {}} 42 self.cfg = self._cfg[title] 43 44 # Get the user's home folder 45 self._home_folder = os.path.expanduser("~") 46 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 47 if not os.path.exists(self._config_folder): 48 os.makedirs(self._config_folder) 49 50 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 51 if os.path.exists(self._config_file): 52 with open(self._config_file, "r") as f: 53 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 54 if title in self._cfg: 55 self.cfg = self._cfg[title] 56 57 def __getitem__(self, tag: str) -> widgets.Widget: 58 return self._widgets[tag] 59 60 def __len__(self) -> int: 61 return len(self._widgets) 62 63 def add_label(self, *args, **kwargs): 64 """ 65 Add a label widget to the container. 66 :param args: args for the widget 67 :param kwargs: kwargs for the widget 68 """ 69 self._nLabels += 1 70 self._widgets[f"label_{self._nLabels}"] = widgets.Label(*args, **kwargs, layout=self._layout, style=self._style) 71 72 def add_button(self, tag, *args, **kwargs): 73 """ 74 Add a button widget to the container. 75 :param tag: tag to identify the widget 76 :param args: args for the widget 77 :param kwargs: kwargs for the widget 78 """ 79 self._widgets[tag] = widgets.Button(*args, **kwargs, layout=self._layout, style=self._style) 80 81 def add_text(self, tag, *args, **kwargs): 82 """ 83 Add a text widget to the container. 84 :param tag: tag to identify the widget 85 :param args: args for the widget 86 :param kwargs: kwargs for the widget 87 """ 88 self._widgets[tag] = widgets.Text(*args, **kwargs, layout=self._layout, style=self._style) 89 90 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 91 """ 92 Add a integer slider widget to the container. 93 :param tag: tag to identify the widget 94 :param args: args for the widget 95 :param remember_value: remember the last value 96 :param kwargs: kwargs for the widget 97 """ 98 if remember_value and tag in self.cfg and kwargs["min"] <= self.cfg[tag] <= kwargs["max"]: 99 kwargs["value"] = self.cfg[tag] 100 self._widgets[tag] = widgets.IntSlider(*args, **kwargs, layout=self._layout, style=self._style) 101 102 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 103 """ 104 Add a float slider widget to the container. 105 :param tag: tag to identify the widget 106 :param args: args for the widget 107 :param remember_value: remember the last value 108 :param kwargs: kwargs for the widget 109 """ 110 if remember_value and tag in self.cfg: 111 kwargs["value"] = self.cfg[tag] 112 self._widgets[tag] = widgets.FloatSlider(*args, **kwargs, layout=self._layout, style=self._style) 113 114 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 115 """ 116 Add a checkbox widget to the container. 117 :param tag: tag to identify the widget 118 :param args: args for the widget 119 :param remember_value: remember the last value 120 :param kwargs: kwargs for the widget 121 """ 122 if remember_value and tag in self.cfg: 123 kwargs["value"] = self.cfg[tag] 124 self._widgets[tag] = widgets.Checkbox(*args, **kwargs, layout=self._layout, style=self._style) 125 126 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 127 """ 128 Add a integer text widget to the container. 129 :param tag: tag to identify the widget 130 :param args: args for the widget 131 :param remember_value: remember the last value 132 :param kwargs: kwargs for the widget 133 """ 134 if remember_value and tag in self.cfg: 135 kwargs["value"] = self.cfg[tag] 136 137 self._widgets[tag] = widgets.IntText( 138 *args, **kwargs, layout=self._layout, style=self._style) 139 140 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 141 """ 142 Add a float text widget to the container. 143 :param tag: tag to identify the widget 144 :param args: args for the widget 145 :param remember_value: remember the last value 146 :param kwargs: kwargs for the widget 147 """ 148 if remember_value and tag in self.cfg: 149 kwargs["value"] = self.cfg[tag] 150 self._widgets[tag] = widgets.FloatText( 151 *args, **kwargs, layout=self._layout, style=self._style) 152 153 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 154 """ 155 Add a dropdown widget to the container. 156 :param tag: tag to identify the widget 157 :param args: args for the widget 158 :param remember_value: remember the last value 159 :param kwargs: kwargs for the widget 160 """ 161 if remember_value and tag in self.cfg and self.cfg[tag] in kwargs["options"]: 162 kwargs["value"] = self.cfg[tag] 163 self._widgets[tag] = widgets.Dropdown(*args, **kwargs, layout=self._layout, style=self._style) 164 165 def add_file_upload(self, tag, *args, accept="*", multiple=False, **kwargs): 166 """ 167 Add a file upload widget to the container. 168 :param tag: tag to identify the widget 169 :param args: args for the widget 170 :param accept: file types to accept 171 :param multiple: allow multiple files to be uploaded 172 :param kwargs: kwargs for the widget 173 """ 174 self._widgets[tag] = FileChooser() 175 176 def save_settings(self): 177 # remember widget values for next time and store them in a config file 178 for tag in self._widgets: 179 if tag.startswith("label_"): 180 pass 181 elif hasattr(self._widgets[tag], "value"): 182 self.cfg[tag] = self._widgets[tag].value 183 self._cfg[self._title] = self.cfg 184 with open(self._config_file, "w") as f: 185 yaml.dump(self._cfg, f) 186 187 def show(self): 188 """ 189 Show the widgets in the container. 190 """ 191 # display the widgets 192 display(*self._widgets.values()) 193 194 def clear(self): 195 """ 196 Clear the widgets in the container. 197 """ 198 self._widgets = {} 199 self._nLabels = 0 200 self._main_display.clear_output()
EasyGui(title='basic_gui', width='50%')
30 def __init__(self, title="basic_gui", width="50%"): 31 """ 32 Container for widgets. 33 :param width: width of the widget container 34 """ 35 self._layout = widgets.Layout(width=width) 36 self._style = {"description_width": "initial"} 37 self._widgets = {} 38 self._nLabels = 0 39 self._main_display = widgets.Output() 40 self._title = title 41 self._cfg = {title: {}} 42 self.cfg = self._cfg[title] 43 44 # Get the user's home folder 45 self._home_folder = os.path.expanduser("~") 46 self._config_folder = os.path.join(self._home_folder, ".nanopyx") 47 if not os.path.exists(self._config_folder): 48 os.makedirs(self._config_folder) 49 50 self._config_file = os.path.join(self._config_folder, "easy_gui.yml") 51 if os.path.exists(self._config_file): 52 with open(self._config_file, "r") as f: 53 self._cfg = yaml.load(f, Loader=yaml.FullLoader) 54 if title in self._cfg: 55 self.cfg = self._cfg[title]
Container for widgets.
Parameters
- width: width of the widget container
def
add_label(self, *args, **kwargs):
63 def add_label(self, *args, **kwargs): 64 """ 65 Add a label widget to the container. 66 :param args: args for the widget 67 :param kwargs: kwargs for the widget 68 """ 69 self._nLabels += 1 70 self._widgets[f"label_{self._nLabels}"] = widgets.Label(*args, **kwargs, layout=self._layout, style=self._style)
Add a label widget to the container.
Parameters
- args: args for the widget
- kwargs: kwargs for the widget
def
add_text(self, tag, *args, **kwargs):
81 def add_text(self, tag, *args, **kwargs): 82 """ 83 Add a text widget to the container. 84 :param tag: tag to identify the widget 85 :param args: args for the widget 86 :param kwargs: kwargs for the widget 87 """ 88 self._widgets[tag] = widgets.Text(*args, **kwargs, layout=self._layout, style=self._style)
Add a text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- kwargs: kwargs for the widget
def
add_int_slider(self, tag, *args, remember_value=False, **kwargs):
90 def add_int_slider(self, tag, *args, remember_value=False, **kwargs): 91 """ 92 Add a integer slider widget to the container. 93 :param tag: tag to identify the widget 94 :param args: args for the widget 95 :param remember_value: remember the last value 96 :param kwargs: kwargs for the widget 97 """ 98 if remember_value and tag in self.cfg and kwargs["min"] <= self.cfg[tag] <= kwargs["max"]: 99 kwargs["value"] = self.cfg[tag] 100 self._widgets[tag] = widgets.IntSlider(*args, **kwargs, layout=self._layout, style=self._style)
Add a integer slider widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_float_slider(self, tag, *args, remember_value=False, **kwargs):
102 def add_float_slider(self, tag, *args, remember_value=False, **kwargs): 103 """ 104 Add a float slider widget to the container. 105 :param tag: tag to identify the widget 106 :param args: args for the widget 107 :param remember_value: remember the last value 108 :param kwargs: kwargs for the widget 109 """ 110 if remember_value and tag in self.cfg: 111 kwargs["value"] = self.cfg[tag] 112 self._widgets[tag] = widgets.FloatSlider(*args, **kwargs, layout=self._layout, style=self._style)
Add a float slider widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_checkbox(self, tag, *args, remember_value=False, **kwargs):
114 def add_checkbox(self, tag, *args, remember_value=False, **kwargs): 115 """ 116 Add a checkbox widget to the container. 117 :param tag: tag to identify the widget 118 :param args: args for the widget 119 :param remember_value: remember the last value 120 :param kwargs: kwargs for the widget 121 """ 122 if remember_value and tag in self.cfg: 123 kwargs["value"] = self.cfg[tag] 124 self._widgets[tag] = widgets.Checkbox(*args, **kwargs, layout=self._layout, style=self._style)
Add a checkbox widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_int_text(self, tag, *args, remember_value=False, **kwargs):
126 def add_int_text(self, tag, *args, remember_value=False, **kwargs): 127 """ 128 Add a integer text widget to the container. 129 :param tag: tag to identify the widget 130 :param args: args for the widget 131 :param remember_value: remember the last value 132 :param kwargs: kwargs for the widget 133 """ 134 if remember_value and tag in self.cfg: 135 kwargs["value"] = self.cfg[tag] 136 137 self._widgets[tag] = widgets.IntText( 138 *args, **kwargs, layout=self._layout, style=self._style)
Add a integer text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_float_text(self, tag, *args, remember_value=False, **kwargs):
140 def add_float_text(self, tag, *args, remember_value=False, **kwargs): 141 """ 142 Add a float text widget to the container. 143 :param tag: tag to identify the widget 144 :param args: args for the widget 145 :param remember_value: remember the last value 146 :param kwargs: kwargs for the widget 147 """ 148 if remember_value and tag in self.cfg: 149 kwargs["value"] = self.cfg[tag] 150 self._widgets[tag] = widgets.FloatText( 151 *args, **kwargs, layout=self._layout, style=self._style)
Add a float text widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_dropdown(self, tag, *args, remember_value=False, **kwargs):
153 def add_dropdown(self, tag, *args, remember_value=False, **kwargs): 154 """ 155 Add a dropdown widget to the container. 156 :param tag: tag to identify the widget 157 :param args: args for the widget 158 :param remember_value: remember the last value 159 :param kwargs: kwargs for the widget 160 """ 161 if remember_value and tag in self.cfg and self.cfg[tag] in kwargs["options"]: 162 kwargs["value"] = self.cfg[tag] 163 self._widgets[tag] = widgets.Dropdown(*args, **kwargs, layout=self._layout, style=self._style)
Add a dropdown widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- remember_value: remember the last value
- kwargs: kwargs for the widget
def
add_file_upload(self, tag, *args, accept='*', multiple=False, **kwargs):
165 def add_file_upload(self, tag, *args, accept="*", multiple=False, **kwargs): 166 """ 167 Add a file upload widget to the container. 168 :param tag: tag to identify the widget 169 :param args: args for the widget 170 :param accept: file types to accept 171 :param multiple: allow multiple files to be uploaded 172 :param kwargs: kwargs for the widget 173 """ 174 self._widgets[tag] = FileChooser()
Add a file upload widget to the container.
Parameters
- tag: tag to identify the widget
- args: args for the widget
- accept: file types to accept
- multiple: allow multiple files to be uploaded
- kwargs: kwargs for the widget
def
save_settings(self):
176 def save_settings(self): 177 # remember widget values for next time and store them in a config file 178 for tag in self._widgets: 179 if tag.startswith("label_"): 180 pass 181 elif hasattr(self._widgets[tag], "value"): 182 self.cfg[tag] = self._widgets[tag].value 183 self._cfg[self._title] = self.cfg 184 with open(self._config_file, "w") as f: 185 yaml.dump(self._cfg, f)
def
view_image(image):
203def view_image(image): 204 """ 205 Plot an image. 206 :param image: image to be plotted 207 """ 208 fig, ax = plt.subplots() 209 fig.canvas.header_visible = False 210 fig.canvas.footer_visible = False 211 212 ax.imshow(image) 213 plt.axis("off") 214 plt.draw()
Plot an image.
Parameters
- image: image to be plotted
def
view_image_stack(image, cmap='viridis'):
217def view_image_stack(image, cmap="viridis"): 218 """ 219 Plot an image stack with dimensions >= 2. 220 Sliders to move across the dimensions are added. 221 :param cmap: colormap to be use to plot the image 222 """ 223 cm = plt.get_cmap(cmap) 224 dims = image.shape 225 params = {} 226 if len(dims) > 2: 227 for i in range(len(dims) - 2): 228 params["dim" + str(i)] = widgets.IntSlider( 229 min=0, max=dims[i] - 1, step=1, value=0, description="dim" + str(i) 230 ) 231 fig, ax = plt.subplots() 232 fig.canvas.header_visible = False 233 fig.canvas.footer_visible = False 234 235 def show_slice(**kwargs): 236 tmp_1 = rescale_intensity(image) 237 for k, value in kwargs.items(): 238 if k != "curtain": 239 tmp_1 = tmp_1[value] 240 ax.imshow(tmp_1, cmap=cmap) 241 plt.axis("off") 242 plt.draw() 243 244 widgets.interact(show_slice, **params)
Plot an image stack with dimensions >= 2. Sliders to move across the dimensions are added.
Parameters
- cmap: colormap to be use to plot the image
def
view_curtain_stack( image_1: numpy.ndarray, image_2: numpy.ndarray, cmap: str = 'viridis'):
247def view_curtain_stack(image_1: np.ndarray, image_2: np.ndarray, cmap: str = "viridis"): 248 """ 249 Plot two image stacks with dimensions >= 2. 250 Sliders to move across the dimensions are added. 251 252 :param image_1: Left image to be plotted on the curtain 253 :param image_2: Right image to be plotted on the curtain 254 :param cmap: Matplotlib colormap to be used to plot the images. Defaults to "viridis". 255 """ 256 assert image_1.shape == image_2.shape 257 dims = image_1.shape 258 params = {} 259 params["curtain"] = widgets.IntSlider( 260 value=image_1.shape[-1] / 2, min=0, max=image_1.shape[-1], description="Curtain" 261 ) 262 if len(dims) > 2: 263 for i in range(len(dims) - 2): 264 params["dim" + str(i)] = widgets.IntSlider( 265 min=0, max=dims[i] - 1, step=1, value=0, description="dim" + str(i) 266 ) 267 268 fig, ax = plt.subplots() 269 fig.canvas.header_visible = False 270 fig.canvas.footer_visible = False 271 272 def show_slice(**kwargs): 273 tmp_1 = rescale_intensity(image_1) 274 tmp_2 = rescale_intensity(image_2) 275 for k, value in kwargs.items(): 276 if k != "curtain": 277 tmp_1 = tmp_1[value] 278 tmp_2 = tmp_2[value] 279 280 for k, value in kwargs.items(): 281 if k == "curtain": 282 combined = np.zeros((image_1.shape[-2], image_1.shape[-1])) 283 combined[:, : int(value)] += tmp_1[:, : int(value)] 284 combined[:, int(value) :] += tmp_2[:, int(value) :] 285 ax.imshow(combined, cmap=cmap) 286 plt.axis("off") 287 plt.draw() 288 289 widgets.interact(show_slice, **params)
Plot two image stacks with dimensions >= 2. Sliders to move across the dimensions are added.
Parameters
- image_1: Left image to be plotted on the curtain
- image_2: Right image to be plotted on the curtain
- cmap: Matplotlib colormap to be used to plot the images. Defaults to "viridis".